package com.liyy.platform.dao;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.liyy.platform.config.RestClientFactory;
import com.liyy.platform.domain.Goods;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortBuilders;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

/**
 * 产品 数据操作类
 */

@Repository
public class ProductRepository{

    @Autowired
    private RestHighLevelClient client;

    //索引库名称
    private static final String INDEX = "product";
    //文档类型
    private static final String TYPE = "product";


    public void saveProduct(Goods goods){
        RestClientFactory.add(INDEX, TYPE, goods.getId().toString(), JSONObject.toJSONString(goods, SerializerFeature.WriteMapNullValue));
    }

    public  void  deleteProduct(Long id){
        RestClientFactory.delete(INDEX, TYPE,id.toString());
    }

    public SearchResponse getProductList(String goodsName, String goodsBrand, String goodsAttr, int pageNo, int pageSize){
        // 这个sourcebuilder就类似于查询语句中最外层的部分。包括查询分页的起始，
        // 查询语句的核心，查询结果的排序，查询结果截取部分返回等一系列配置
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        // 结果开始处
        sourceBuilder.from((pageNo-1)*pageSize);
        // 查询结果终止处
        sourceBuilder.size(pageSize);
        // 查询的等待时间
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        //开始拼接查询语句
        MatchQueryBuilder matchGoodsName = QueryBuilders.matchQuery("goodsName",goodsName);
        MatchQueryBuilder matchGoodsBrand = QueryBuilders.matchQuery("goodsBrand",goodsBrand);
        MatchQueryBuilder matchGoodsAttr = QueryBuilders.matchQuery("goodsAttr",goodsAttr);
        // 等同于bool，将3个查询合并
        BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
        boolBuilder.should(matchGoodsName);
        boolBuilder.should(matchGoodsBrand);
        boolBuilder.should(matchGoodsAttr);
        // 排序
        FieldSortBuilder fsb = SortBuilders.fieldSort("_score");
        fsb.order(SortOrder.DESC);
        sourceBuilder.sort(fsb);

        sourceBuilder.query(boolBuilder);
        System.out.println(sourceBuilder);
        //指定更新的索引库和类型
        SearchRequest searchRequest = new SearchRequest(INDEX);
        searchRequest.types(TYPE);
        searchRequest.source(sourceBuilder);
        try {
            return client.search(searchRequest);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    public SearchResponse getAllList(int pageNo, int pageSize){
       return RestClientFactory.getAllList(INDEX,TYPE,pageNo,pageSize);
    }

}

